<head>


        <script type="text/javascript">
            var addEvent = (function () {
                if (document.addEventListener) {
                    return function (el, type, fn) {
                        el.addEventListener(type, fn, false);
                    };
                } else {
                    return function (el, type, fn) {
                        el.attachEvent('on' + type, function () {
                            return fn.call(el, window.event);
                        });
                    }
                }
            })();
            var deliver = function(iframe){
                addEvent(iframe.contentWindow,'blur',function(){
                    var tip = document.getElementById("exe");
                    iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
                    tip.innerHTML = "失去焦点，并取得iframe的内容为："+iframeDocument.body.innerHTML
                    tip.style.backgroundColor = "#9999CC";
                })
            }
	    
	    
	    function  buttonClick(){
	    
	    alert("ffffffffffffffffffffffff");
		document.getElementById("textarea").value=iframeDocument.body.innerHTML;
		
		} 
            window.onload = function(){
                var editor = document.getElementById("editor");
                var buttons = document.getElementById("image");
                var selects = editor.getElementsByTagName("select");
                var textarea = document.getElementById("textarea");
                textarea.style.display = "none";
                var iframe = document.createElement("iframe");
                iframe.style.width = "390px";
                iframe.style.height = "100px";
                iframe.frameBorder=0;
                textarea.parentNode.insertBefore(iframe,textarea);
                var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
                iframeDocument.designMode = "on";
                iframeDocument.open();
                iframeDocument.write('<html><head><style type="text/css">body{ font-family:arial; font-size:13px;background:#DDF3FF }</style></head></html>');
                iframeDocument.close();
                var switchEditMode = true;
              
                    buttons.onclick = new function(){
                    
                  
                        var command=buttons.getAttribute("title");
                        return function(){
                            switch(command){
                                
                                case 'insertimage':
                                    var value = prompt('请输入超链接:', 'http://');
                                    iframeDocument.execCommand(command,false,value);
                                    break;
                               
                          if(switchEditMode){//切换到textarea
                                        iframe.style.display = "none";
                                        textarea.style.display = "block";
                                        textarea.value = iframeDocument.body.innerHTML;
                                        textarea.focus();
                                        switchEditMode = false;
                                    }else{//切换到iframe
                                        iframe.style.display = "block";
                                        textarea.style.display = "none";
                                        iframeDocument.body.innerHTML = textarea.value;
                                        iframe.contentWindow.focus();
                                        switchEditMode = true;
                                    }
                                    break;
                                default:
                                    iframeDocument.execCommand(command,false,'');
                                    iframe.contentWindow.focus();
                            }
                        };
                    };
                
               
                deliver(iframe);
            }
        </script>
</head>


<%= form_for(@test) do |f| %>
  <% if @test.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@test.errors.count, "error") %> prohibited this test from being saved:</h2>

      <ul>
      <% @test.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
  
          <p id="exe"></p>
        <div id="editor">

            <span class="button" title="insertimage" id="image" unselectable="on">ͼƬ</span>
            
        </div>
        <br style="clear:both"/>
        <div id="textarea"></div>

          <p>ccccccccc</p>
 
    <%= f.text_field :name ,:id =>"kindeditor"%>
  </div>
  <div class="actions">
   <%= f.submit "123", :onclick => "return buttonClick();" %>
  </div>
<% end %>
